import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:h_flutter_119/api/Api.dart';
import 'package:h_flutter_119/http/httpUtils.dart';
import 'package:h_flutter_119/res/colors.dart';
import 'package:h_flutter_119/res/style.dart';

import '../../find/model/Focus_Entity.dart';
import '../../find/model/Focus_Item_Entity.dart';

typedef ItemClickCallback = void Function(String title, String videoUrl);

class VideoListItemWidget extends StatefulWidget {
  String videoId;
  ItemClickCallback _itemClickCallback;

  @override
  State<StatefulWidget> createState() => _VidieoItemWidgetState();

  VideoListItemWidget(this.videoId, this._itemClickCallback, {Key? key})
      : super(key: key);
}

class _VidieoItemWidgetState extends State<VideoListItemWidget> {
  List<FocusItemEntity> _data = [];
  int _selectIndex = -1;

  @override
  void initState() {
    super.initState();
    _getVideoDetail();
  }

  @override
  Widget build(BuildContext context) {
    return _data.isEmpty? const SizedBox():ListView.builder(
      itemBuilder: (context, index) => GestureDetector(
          onTap: () {
            if(index == _selectIndex) {
              return;
            }
            setState((){
              _selectIndex = index;
            });
            widget._itemClickCallback(_data[index].data?.title??"", _data[index].data?.playUrl??"");
          },
          child: Container(
            color: Colors.white,
            padding: EdgeInsets.symmetric(horizontal: 20.w),
            height: 180.w,
            child: Row(
              children: [
                Image.network(_data[index].data?.cover?.feed ?? "",
                    height: 160.w, width: 250.w),
                Box.wBox10,
                Expanded(
                    child: Column(
                  children: [
                    Text(
                      _data[index].data?.title ?? "",
                      style: TextStyle(
                          fontSize: 28.w,
                          color: _selectIndex == index
                              ? ColorStyle.color_EA4C43
                              : ColorStyle.color_333333),
                      overflow: TextOverflow.ellipsis,
                      maxLines: 2,
                    ),
                    Text(_data[index].data?.description??"",
                    style: TextStyle(color: _selectIndex == index
                        ? ColorStyle.color_EA4C43
                        : ColorStyle.color_999999, fontSize: 24.w),
                    overflow: TextOverflow.ellipsis,maxLines: 2,)
                  ],
                )),
              ],
            ),
          )),
      itemCount: 5,
    );
  }

  void _getVideoDetail() async {
    var response = await HttpUtils.getInstance()
        .get(Api.VIDEO_DETAIL, data: {"id": widget.videoId.toString()});
    var map = json.decode(response.toString());
    FocusEntity entity = FocusEntity.fromJson(map);
    setState(() {
      var itemList = entity.itemList ?? [];
      var tempList = <FocusItemEntity>[];
      for (var element in itemList) {
        if (element.type == "videoSmallCard") {
          tempList.add(element);
        }
      }
      _data.addAll(tempList);
    });
  }
}
